package cn.edu.neusoft.yuejian.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import cn.edu.neusoft.yuejian.R;

import cn.edu.neusoft.yuejian.bean.activity;
import cn.edu.neusoft.yuejian.frgment.HdFragment;
import cn.edu.neusoft.yuejian.model.yuemodel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HdInfoActivity extends AppCompatActivity {
     private Button bt_bm;
    private ImageButton ImageB_fh;
    private ImageView pic;
    private TextView name,mc,fqz,dz,sj,fy,rs,dh,jj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hd_info);
        initViews();
        initEvents();
        initData();
    }

    private void initViews() {
        bt_bm=(Button)findViewById(R.id.bu_info_bm);
        ImageB_fh=(ImageButton)findViewById(R.id.hd_inifo_btn_fh);
        name=(TextView)findViewById(R.id.hd_info_name);
        pic=(ImageView)findViewById(R.id.hd_info_pic);
        mc=(TextView)findViewById(R.id.hd_info_mc);
        fqz=(TextView)findViewById(R.id.hd_info_fqz);
        dz=(TextView)findViewById(R.id.hd_info_dz);
       sj=(TextView)findViewById(R.id.hd_info_sj);
       fy=(TextView)findViewById(R.id.hd_info_fy);
        rs=(TextView)findViewById(R.id.hd_info_rs);
        dh=(TextView)findViewById(R.id.hd_info_dh);
       jj=(TextView)findViewById(R.id.hd_info_jj);
    }

    private void initEvents() {
        bt_bm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HdInfoActivity.this, BuyActivity.class);

                startActivity(intent);
            }
        });
        ImageB_fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HdInfoActivity.this,  MainActivity.class);


                startActivity(intent);
            }
        });

    }

    private void initData() {
        Bundle intent=getIntent().getExtras();
        String id = intent.getString("id");
        yuemodel yuemodel=new yuemodel();
        Call<activity> call= yuemodel.getactivitybyId(id);
        call.enqueue(new Callback<activity>() {
            @Override
            public void onResponse(Call<activity> call, Response<activity> response) {
                activity activity=response.body();
              name.setText(activity.getActivity_name());
               mc .setText(activity.getActivity_name());
                Picasso.with(HdInfoActivity.this)
                        .load("http://172.24.10.175/workout/Uploads/"+activity.getThumb())
                        .transform(new CropSquareTransformation())
                        .error(R.drawable.wu)
                        .into(pic);
               fqz.setText(activity.getInsert_time()+""+activity.getClub_name());
                dz.setText("地址："+activity.getCity_name()
                        +activity.getDistrict_name()+activity.getPlace());
                sj.setText("时间："+activity.getStart_date()+activity.getEnd_date());
                fy .setText("活动费用:"+activity.getActivity_price()+"元/人");
                rs.setText("已报名人数:"+activity.getActivity_price_stu());
                dh.setText("咨询热线:"+activity.getContact_tel());
                jj.setText(activity.getIntroduction());

            }

            @Override
            public void onFailure(Call<activity> call, Throwable t) {

            }
        });

    }
    public class CropSquareTransformation implements Transformation {
        @Override public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
            Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
            if (result != source) {
                source.recycle();
            }
            return result;
        }

        @Override public String key() { return "square()"; }
    }
    }


